﻿using lwzlz.Application.System;
using lwzlz.Application.System.Dto;
using lwzlz.Core;
using lwzlz.Web.Areas.SysAdmin.Models;
using System;
using System.Web.Mvc;

namespace lwzlz.Web.Areas.SysAdmin.Controllers
{
    public class MenuController : AdminController
    {
        #region 首页
        public ActionResult Index()
        {
            return View();
        }
        #endregion

        #region 获得菜单首页列表
        [HttpPost]
        public JsonResult GetMenuList()
        {
            LayTableModel result = new LayTableModel();
            MenuService service = new MenuService();
            try
            {
                var conditions = new
                {
                    MenuType = 1,
                    IsDeleted = false
                };
                var list = service.GetMenuList(conditions);

                result.data = list;
            }
            catch (Exception ex)
            {
                result.code = -1;
                result.msg = ex.Message;
            }
            return Json(result);
        }
        #endregion

        #region 菜单详细显示页面
        public ActionResult MenuAddOrEdit(int id = 0)
        {
            MenuOutput model = null;
            MenuService service = new MenuService();
            if (id > 0)
            {
                model = service.GetMenu(id);
            }
            else
            {
                model = new MenuOutput();
                model.IsEnable = true; // 默认启用
                int parentId = 0;
                if (int.TryParse(Request.Params["parentId"], out parentId))
                {
                    model.ParentId = parentId;
                }
            }
            ViewBag.ParentList = service.GetMenuList(new
            {
                MenuType = 1,
                IsDeleted = false,
                IsEnable = true
            });
            return View(model);
        }
        #endregion

        #region 菜单添加和修改
        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult MenuAddOrEdit(MenuInput model)
        {
            ResponseModel response = new ResponseModel();
            if (model.Id > 0)
            {
                response = MenuEdit(model);
            }
            else
            {
                response = MenuAdd(model);
            }
            return Json(response);
        }
        private ResponseModel MenuAdd(MenuInput model)
        {
            ResponseModel response = new ResponseModel();
            MenuService serviece = new MenuService();
            try
            {
                int? id = serviece.AddMenu(model);

                response.Data = id.Value;
                response.Success = true;
                response.Message = "添加成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return response;
        }

        private ResponseModel MenuEdit(MenuInput model)
        {
            ResponseModel response = new ResponseModel();
            MenuService serviece = new MenuService();
            try
            {
                int number = serviece.EidtMenu(model);

                response.Success = true;
                response.Message = "修改成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return response;
        }
        #endregion

        #region 按钮首页
        public ActionResult ButtonList(int id)
        {
            ViewBag.MenuID = id;
            return View();
        }
        #endregion

        #region 获得按钮首页列表
        [HttpPost]
        public JsonResult GetButtonList(int id)
        {
            LayTableModel result = new LayTableModel();
            MenuBtnService service = new MenuBtnService();
            try
            {
                var conditions = new
                {
                    ParentId = id,
                    MenuType = 2,
                    IsDeleted = false,
                };
                var list = service.GetButtonList(conditions);

                result.data = list;
            }
            catch (Exception ex)
            {
                result.code = -1;
                result.msg = ex.Message;
            }
            return Json(result);
        }
        #endregion

        #region 详细显示页面
        [HttpGet]
        public ActionResult ButtonAddOrEdit(int id = 0)
        {
            ButtonOutput model = null;
            int menuId = 0; // 菜单ID
            if (!int.TryParse(Request.Params["menuId"], out menuId))
            {
                throw new InvalidCastException("无效的菜单ID");
            }
            if (id > 0)
            {
                MenuBtnService service = new MenuBtnService();
                model = service.GetButton(id);
            }
            else
            {
                model = new ButtonOutput();
                model.IsEnable = true; // 默认启用
            }
            ViewBag.MenuID = menuId;
            return View(model);
        }
        #endregion

        #region 按钮添加和修改
        [HttpPost]
        [ValidateInput(false)]
        [ValidateAntiForgeryToken]
        public JsonResult ButtonAddOrEdit(ButtonInput model)
        {
            ResponseModel response = new ResponseModel();
            if (model.Id > 0)
            {
                response = ButtonEdit(model);
            }
            else
            {
                response = ButtonAdd(model);
            }
            return Json(response);
        }

        private ResponseModel ButtonAdd(ButtonInput model)
        {
            ResponseModel response = new ResponseModel();
            MenuBtnService serviece = new MenuBtnService();
            try
            {
                int? id = serviece.AddButton(model);

                response.Data = id.Value;
                response.Success = true;
                response.Message = "添加成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return response;
        }

        private ResponseModel ButtonEdit(ButtonInput model)
        {
            ResponseModel response = new ResponseModel();
            MenuBtnService serviece = new MenuBtnService();
            try
            {
                int number = serviece.EditButton(model);

                response.Success = true;
                response.Message = "添加成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return response;
        }
        #endregion

        #region 删除
        public JsonResult Delete(int id)
        {
            ResponseModel response = new ResponseModel();
            MenuService service = new MenuService();
            try
            {
                service.Delete(id);

                response.Success = true;
                response.Message = "删除成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }
        #endregion

        #region 修改是否启用
        public JsonResult IsEnable(int id, bool check)
        {
            ResponseModel response = new ResponseModel();
            MenuService service = new MenuService();
            Menu model = service.GetMenuModel(id);
            try
            {
                model.IsEnable = (check == true ? true : false);
                int count = service.UpdateIsEnable(model);
                if (count > 0)
                {
                    response.Success = true;
                    response.Message = "更新成功";
                }
                else
                {
                    response.Success = false;
                    response.Message = "更新失败";
                }

            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }
        #endregion
    }
}